package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController//==》responseBody+controller
@RequestMapping("/checkItem")
public class CheckItemController {

    @Reference
    private CheckItemService checkItemService;
    //保存功能
    @RequestMapping("/add")
    @PreAuthorize("hasAuthority('CHECKITEM_ADD')")
    public Result save(@RequestBody CheckItem checkItem){
        try {
            checkItemService.save(checkItem);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }
        return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }
    //第一次访问
    @RequestMapping("/findAll")
    public PageResult findAllByPage(@RequestBody QueryPageBean pageBean){
        PageResult pageResult =  checkItemService.findByPage(pageBean);
        return pageResult;
    }
    @RequestMapping("/deleteById")
    @PreAuthorize("hasAuthority('CHECKITEM_DELETE')")
    public Result deleteById(Integer id){

        Result result = checkItemService.deleteById(id);
        return result;
    }
    @RequestMapping("/updateShow")
    public Result updateShow(Integer id){
        System.out.println(id);
        Result result = checkItemService.findById(id);
        System.out.println();
        return result;
    }
    @RequestMapping("/update")
    @PreAuthorize("hasAuthority('CHECKITEM_EDIT')")
    public Result update(@RequestBody  CheckItem checkItem){
        Result result = null;
        try {
            result = checkItemService.update(checkItem);
        } catch (AccessDeniedException e) {
           return new Result(false,"没有权限");
        }
        return result;
    }
    @RequestMapping("/findAll2")
    public Result findAll(){
        Result result = checkItemService.findAll();
        return result;
    }
}
